﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using hedefgrup.reports.common;
using System.Text;

public partial class r_face_filters : System.Web.UI.Page
{
    Report_Abstract reportInstance = null;

    private void FillFilters()
    {
        foreach (Filter filter in reportInstance.Filters)
        {
            string currentFilter = Request.Form["Filters"];
            ListItem filterItem = new ListItem(ToFilterText(filter.Name), filter.Name);
            if (currentFilter == filter.Name)
            {
                filterItem.Selected = true;
            }
            Filters.Items.Add(filterItem);
        }
    }

    private string ToFilterText(string currentFilter)
    {
        hedefgrup.reports.common.Tree questionnaire = hedefgrup.reports.common.Report_Abstract.Current.Context.Questionnaire;
        hedefgrup.reports.common.Tree question = questionnaire.GetChild(currentFilter);
        if (question != null)
        {
            return currentFilter + ": " + question.Text;
        }
        else
        {
            return currentFilter;
        }
    }

    private void FillFilterValues()
    {
        string currentFilter = Filters.Value;
        if (!string.IsNullOrEmpty(currentFilter))
        {
            Filter selectedFilter = reportInstance.GetFilterByName(currentFilter);

            if (selectedFilter.ValueType != typeof(string))
            {
                FilterValue.Attributes.Add("disabled", "disabled");
            }

            hedefgrup.reports.common.Tree questionnaire = hedefgrup.reports.common.Report_Abstract.Current.Context.Questionnaire;
            hedefgrup.reports.common.Tree question = questionnaire.GetChild(currentFilter);

            if (question != null && currentFilter != "BAYII")
            {
                foreach (hedefgrup.reports.common.Tree option in question.Children)
                {
                    FilterValueList.Items.Add(new ListItem(option.Key + ": " + option.Text, option.Key));
                }
            }
            else
            {
                foreach (string filterValue in selectedFilter.GetValueList())
                {
                    FilterValueList.Items.Add(new ListItem(filterValue, filterValue));
                }
            }
        }
    }

    private void FillAppliedFilters()
    {
        StringBuilder appliedFiltersBuilder = new StringBuilder();
        foreach (Filter appliedFilter in reportInstance.GetAppliedFilters())
        {
            hedefgrup.reports.common.Tree questionnaire = hedefgrup.reports.common.Report_Abstract.Current.Context.Questionnaire;
            StringBuilder valuesBuilder = new StringBuilder();
            hedefgrup.reports.common.Tree question = questionnaire.GetChild(appliedFilter.Name);

            foreach (string value in appliedFilter.Values)
            {
                if (question != null)
                {
                    hedefgrup.reports.common.Tree option = question.GetChild(value);
                    if (option != null)
                    {
                        valuesBuilder.AppendFormat("{0}: {1}<br/>", option.Key, option.Text);
                    }
                    else
                    {
                        valuesBuilder.AppendFormat("{0}<br/>", value);
                    }
                }
                else
                {
                    valuesBuilder.AppendFormat("{0}<br/>", value);
                }
            }
            valuesBuilder.Length -= 5;

            appliedFiltersBuilder.AppendFormat
            (
                "<tr><td>{2}</td><td>{1}</td><td><a href=\"javascript:void\" onclick=\"javascript:removeFilter('{0}');\"><img src=\"/r-face/images/delete.png\" width=\"24\" height=\"24\" /></a>",
                appliedFilter.Name,
                valuesBuilder,
                ToFilterText(appliedFilter.Name)
            );
        }
        AppliedFilters.Text = appliedFiltersBuilder.ToString();
    }

    private void AddFilter()
    {
        if (Request.Form["AddFilter"] == "1")
        {
            Filter filterToAdd = reportInstance.GetFilterByName(Request.Form["Filters"]);
            if (filterToAdd != null)
            {
                if (string.IsNullOrEmpty(Request.Form["FilterValue"]))
                {
                    if (Request.Form["FilterValueList"] == null)
                    {
                        WarningMessage.Text = "Filtre değeri seçilmelidir.";
                    }
                    else
                    {
                        filterToAdd.Values.Add(Request.Form["FilterValueList"]);
                    }
                }
                else
                {
                    filterToAdd.Values.Add("%" + Request.Form["FilterValue"] + "%");
                }
            }
            else
            {
                WarningMessage.Text = "Filtre seçilmelidir.";
            }
        }
    }

    private void RemoveFilter()
    {
        string filterNameToRemove = Request.Form["FilterToRemove"];
        if (!string.IsNullOrEmpty(filterNameToRemove))
        {
            Filter filterToRemove = reportInstance.GetFilterByName(filterNameToRemove);
            if (filterToRemove != null)
            {
                filterToRemove.Values.Clear();
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        reportInstance = Report_Abstract.Current;

        FillFilters();

        FillFilterValues();

        AddFilter();

        RemoveFilter();

        FillAppliedFilters();
    }
}